package com.define.commons.common.utils;

import com.define.commons.common.enums.HttpStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回格式封装类
 * @Author xiewenlong
 * @Date 2018/11/9 9:19
 */
@Data
@JsonPropertyOrder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RS<T extends Object> implements Serializable {
    private static final long serialVersionUID = 3765186806061023354L;

    private Integer code;
    private String msg;
    private String errorDetail;
    private T data;

    /**  -------------------- Constructor Start -------------------- */
    public RS(){}
    public RS(T data){
        this(HttpStatus.OK);
        this.data = data;
    }
    public RS(T data, Class clazz) throws IllegalAccessException, InstantiationException {
        this(HttpStatus.OK);
        if(data == null){
            this.data = (T) clazz.newInstance();
        }else{
            this.data = data;
        }
    }
    public RS(Exception e){
        this(HttpStatus.ERROR.getCode(),
                HttpStatus.ERROR.getMsg(),
                null, e.toString());
    }
    public RS(HttpStatus httpStatus){
        this(httpStatus.getCode(), httpStatus.getMsg());
    }
    public RS(Integer code, T data){
        this(data);
        this.msg = HttpStatus.getMsgByCode(code);
        this.code = code;
    }
    public RS(Integer code, String msg, T data){
        this(code, msg);
        this.data = data;
    }
    public RS(Integer code, String msg){
        this.code = code;
        this.msg = msg;
    }
    public RS(Integer code, String msg, String errorDetail){
        this(code, msg);
        this.errorDetail = errorDetail;
    }
    public RS(Integer code, String msg, T data, String errorDetail){
        this(code, msg, errorDetail);
        this.data = data;
    }
    /**  -------------------- Constructor End -------------------- */

    /**  -------------------- CommonUse Start -------------------- */
    /** 成功 */
    public static RS ok() {
        return new RS(HttpStatus.OK);
    }
    /** 用户发出的请求是不存在的记录 */
    public static RS notFound() {
        return new RS(HttpStatus.NOT_FOUND);
    }
    /** 请求无效 */
    public static RS badRequest() {
        return new RS(HttpStatus.BAD_REQUEST);
    }
    /** 访问是被禁止 */
    public static RS forbidden() {
        return new RS(HttpStatus.FORBIDDEN);
    }
    /** 表示用户没有权限（令牌、用户名、密码错误）*/
    public static RS unauthorized() {
        return new RS(HttpStatus.UNAUTHORIZED);
    }
    /** 服务器发生错误 */
    public static RS serverInternalError() {
        return new RS(HttpStatus.ERROR);
    }
    /**  -------------------- CommonUse End -------------------- */

    /**  -------------------- Connection Start -------------------- */
    public RS code(Integer code){
        this.code = code;
        return this;
    }
    public RS msg(String msg){
        this.msg = msg;
        return this;
    }
    public RS errorDetail(String errorDetail){
        this.errorDetail = errorDetail;
        return this;
    }
    public RS data(T data){
        this.data = data;
        return this;
    }
    /**  -------------------- Connection End -------------------- */
}
